package com.colin.aiyi.ui.register;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.colin.aiyi.R;
import com.colin.aiyi.app.Constant;
import com.colin.aiyi.app.MyApp;
import com.colin.aiyi.entity.User;
import com.colin.aiyi.entity.response.BaseResponse;
import com.colin.aiyi.ui.BaseActivity;
import com.colin.aiyi.ui.PasswdSettingAc;
import com.colin.aiyi.ui.login.LoginAc;
import com.colin.aiyi.utils.TimeUtil;
import com.colin.aiyi.utils.VerifyUtil;
import com.colin.aiyi.view.ShowMsgUtil;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by hl on 2017/5/19.
 */

public class RegisterAc extends BaseActivity implements View.OnClickListener, TimeUtil.TimeDownEvent {
    @BindView(R.id.edit_phone)
    EditText editPhone;
    @BindView(R.id.edit_vcode)
    EditText editVcode;
    @BindView(R.id.img_logo)
    ImageView imgLogo;
    @BindView(R.id.button_vcode)
    Button buttonVcode;
    boolean flagTime = true;
    @BindView(R.id.text_login)
    TextView textLogin;
    @BindView(R.id.text_msg)
    TextView textMsg;
    @BindView(R.id.text_banner_left)
    TextView textBannerLeft;
    @BindView(R.id.text_banner_title)
    TextView textBannerTitle;
    @BindView(R.id.text_banner_rigth)
    TextView textBannerRigth;
    @BindView(R.id.linear)
    LinearLayout linear;
    @BindView(R.id.text_title)
    TextView textTitle;
    @BindView(R.id.text_register)
    TextView textRegister;
    @BindView(R.id.text_login_wx)
    TextView textLoginWx;
    private String phoneNumber;
    private String strEidtVcode;
    private long firstTime;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_register);
        ButterKnife.bind(this);
        textLogin.setText(Html.fromHtml("已有帐号 <u>登录</u>"));
        user = new User();
        linear.setBackgroundColor(Color.alpha(100));
        textBannerLeft.setOnClickListener(this);
        textBannerTitle.setText("");

      /*  mLoginListener = new LoginListener() {
            @Override
            public void loginSuccess(LoginResult result) {
                Toast.makeText(RegisterAc.this,
                        result.getUserInfo() != null ? result.getUserInfo().getNickname()
                                : "" + "登录成功", Toast.LENGTH_SHORT).show();

                // 处理result
                switch (result.getPlatform()) {
                    case LoginPlatform.QQ:
                        QQUser user = (QQUser) result.getUserInfo();
                        QQToken token = (QQToken) result.getToken();
                        break;
                    case LoginPlatform.WEIBO:
                        // 处理信息
                        break;
                    case LoginPlatform.WX:

                        break;
                }
            }

            @Override
            public void loginFailure(Exception e) {
                Toast.makeText(RegisterAc.this, "登录失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void loginCancel() {
                Toast.makeText(RegisterAc.this, "登录取消", Toast.LENGTH_SHORT).show();
            }
        };*/

    }

    @Override
    protected void onResume() {
        super.onResume();
        iPresenter = new ImpPRegister();
    }



    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.text_login_wx:
                if (System.currentTimeMillis()-firstTime<2000){
                    firstTime=System.currentTimeMillis();
                    return;
                }
                ShowMsgUtil.showLoadingDialog(this);
                IWXAPI api= WXAPIFactory.createWXAPI(this,Constant.APP_ID);
                api.registerApp(Constant.APP_ID);
                final SendAuth.Req req = new SendAuth.Req();
                req.scope = "snsapi_userinfo";
                req.state = "none";
                api.sendReq(req);
                break;
            case R.id.text_login:

                startActivity(new Intent(this, LoginAc.class));
                finish();
                break;
            case R.id.text_register:
                if (VerifyUtil.checkTextValueRight(new TextView[]{editPhone, editVcode},
                        new String[]{"请输入正确手机号", "请输入验证码"},
                        new String[]{VerifyUtil.EX_PHONE, VerifyUtil.EX_NAME},
                        textMsg)) {
                    phoneNumber = editPhone.getText().toString().trim();
                    strEidtVcode = editVcode.getText().toString().trim();
                    Intent intent = new Intent(this, PasswdSettingAc.class);
                    intent.putExtra("vcode", strEidtVcode);
                    intent.putExtra("phone", phoneNumber);
                    startActivity(intent);
                }
                break;
            case R.id.button_vcode:
                if (!flagTime) {
                    return;
                }
                if (VerifyUtil.checkTextValueRight(new TextView[]{editPhone},
                        new String[]{"请输入正确手机号"},
                        new String[]{VerifyUtil.EX_PHONE},
                        textMsg)) {
                    phoneNumber = editPhone.getText().toString().trim();
                    if (flagTime) {
                        TimeUtil.timeDown(Constant.TIMER, this);
                        flagTime = false;
                    }
                    user.setTelNo(editPhone.getText().toString());
                    iPresenter.register_sendCode(user);
                }
                break;
        }
    }


    private boolean chenkValueRigth() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void msgProcesse(BaseResponse baseResponse) {
        ShowMsgUtil.hideLoadingDialog();
        if (baseResponse instanceof BaseResponse) {
            showMsg("短信已发送到" + phoneNumber);
        }
    }

    @Override
    public void timeDown(int indexTime) {
        if (indexTime == -1) {
            flagTime = true;
            buttonVcode.setText("发送");
            return;
        }

        indexTime = Constant.TIMER - indexTime;
        indexTime = indexTime / 1000;
        buttonVcode.setText(indexTime + "秒");
    }


   private void senlogin() {
        SendAuth.Req req=new SendAuth.Req();
        req.scope="snsapi_userinfo";
        req.state="wctest";
        MyApp.getInstance().getApi().sendReq(req);
    }
}
